#+TITLE:Neo4j

* CSV Example

Currently source data for our “Calls” Data Model stored in CSV file. You can
download the csv file here: https://vbatushkov.bitbucket.io/log_of_calls.csv

#+BEGIN_SRC cypher
//Enable multi statement query editor in Database Settings (!)
MATCH (n) DETACH DELETE n;
LOAD CSV WITH HEADERS FROM 'https://vbatushkov.bitbucket.io/log_of_calls.csv' AS line
MERGE (c1:City { name: line.from_city })
MERGE (p1:Person { name: line.from_name, number: line.from_number, gender: line.from_gender })
MERGE (p1)-[:FROM]->(c1)
MERGE (c2:City { name: line.to_city })
MERGE (p2:Person { name: line.to_name, number: line.to_number, gender: line.to_gender })
MERGE (p2)-[:FROM]->(c2)
CREATE (c:Call { from: datetime(line.from_dt),
		 to: datetime(line.to_dt),
                 duration: duration.between(datetime(line.from_dt), datetime(line.to_dt)).minutes })
CREATE (p1)-[:OUT]->(c)<-[:IN]-(p2);
#+END_SRC

** Cypher Lang
Cypher — is a declarative query language, built on the basic concepts and
clauses of SQL but with added graph-specific functionality. And the main idea to
understand is a concept of Graph Pattern Matching.

*** Pattern Matching
#+BEGIN_SRC cypher
MATCH (p:Perso)-[:FROM]->(c:City)
WHERE p.name = "Natalia"
RETURN p, c
#+END_SRC

#+RESULTS:
: zsh:1: command not found: neo4j-shell


** Queries :ATTACH:
:PROPERTIES:
:ID:       16fb8dc2-8975-41b6-b248-8fc57a20ecb4
:END:



[[download:16/fb8dc2-8975-41b6-b248-8fc57a20ecb4/_20200324_0132171*_kAIBuPWOmwWP3kaySiRww.png]]


[[download:16/fb8dc2-8975-41b6-b248-8fc57a20ecb4/_20200324_0132351*g_O-BxzcD34oAJr_T2ZWjw.png]]

[[download:16/fb8dc2-8975-41b6-b248-8fc57a20ecb4/_20200324_0132391*QDxK9bniPHAc8mutDdxplQ.png]]

** Quiz
    How many calls were missed in May?
    Find the name of man, who received a call from Tiffany in May?
    Find a city with the lowest number of internal city calls?
    How many women from Pattaya received calls from Bangkok men?
    On date 25 of April, find the woman who has the least total duration of conversations?
    How many pairs of people, where persons called to each other?
https://medium.com/@vladbatushkov/learn-neo4j-cypher-basics-in-30-minutes-94d68a52544

* Relationship between Companies and Universities
We want to create a Database of Companies who are affiliated with Governmental
Institutions (eg. Uni) for this we need Companies and Universities

** Create
#+BEGIN_SRC cypher
// Cypher code for a Donation
CREATE (p:Company {name:"Company"}) - [:DONATION {sum:16000}] -> (u:University {name:"Universität Rostock"})
#+END_SRC
This Example Query Create a Company and an University. They share the
relationship Donation. Companies, Donations and Universties have Properies which
are declared

It is also possible to create a single Nodes with Properies and then create the relationship
#+BEGIN_SRC cypher
CREATE (hs:University {name:"Hochschule Bremen" , adresse:"Neustadtswall 30, 28199 Bremen"})
CREATE (r:Company {name:"Rheinmetal", adress:"Rheinmetall Platz 1, 40476 Düsseldorf, DE", companyNumber:"R1101_HRB39401",
                        qid:"Q161544"} )
CREATE (r) - [:DONATION {sum:100000, reason:"Spende Deutschlandlandstipendium 2018/2019"}] -> (hs)
#+END_SRC
o
** Query

*** Find all Nodes
If we want to return all nodes we use match and query without constraints
#+BEGIN_SRC cypher
MATCH (n) RETURN (n)
#+END_SRC

*** Find node with specific property
#+BEGIN_SRC cypher
// Return all Companies
MATCH (n:Company) RETURN (n)
#+END_SRC

*** How many Nodes do we have
#+BEGIN_SRC cypher
// Count all nodes
MATCH (n)
RETURN count(n)
#+END_SRC

*** How many Nodes do we have of a specific Type
Now we just want to a type of Node
#+BEGIN_SRC cypher
// Count all nodes which are Companies
MATCH (n:Company)
RETURN count(n)
#+END_SRC

** Visualisation
*** What is our Schema
Databases can be confusing. A Database visualisation helps us understand our
data model. In this example there is currently just one Relationship between
Companies and Universities.
#+BEGIN_SRC cypher
CALL db.schema.visualization
#+END_SRC

*** Find all Donations and return its relationship
Here we want to find all Donations and then visualize the result for further
understanding, note that we only show 25 entries for performance reasons
#+BEGIN_SRC cypher
MATCH p=()-[r:DONATION]->() RETURN p LIMIT 25
#+END_SRC
Notice the empty parenthesises, they mean that we use wildcards. In our context
we dont care what the origin node or what target node is. We only care about the
fact that the relationship is a _DONATION_
*** Return just the relationships
#+BEGIN_src cypher
MATCH p=()-[r:DONATION]->() RETURN r LIMIT 25
#+END_SRC
The difference here is, that we now just return the relationship. This is
meaningless for us, since we do not now the context in which this relationship exists
